package com.test3;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.*;
import java.math.BigDecimal;
import java.net.URL;
import java.util.Calendar;

import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStream;

import org.apache.commons.lang3.StringUtils;

public class Test1 {
	
	public static void main(String[] args) throws IOException {

	    File aa = new File("1");
		
		BufferedImage im1 = ImageIO.read(new File("C:/Users/domains90/Desktop/qrcode_258.png")); 
		BufferedImage im2 = ImageIO.read(new File("C:/Users/domains90/Desktop/qrcode_430.png")); 
		BufferedImage im3 = ImageIO.read(new File("C:/Users/domains90/Desktop/qrcode_1280.png"));
		
		if(im1.getWidth()<=361) {
			
		}else if(im1.getWidth()<=603) {
			
		}else if(im1.getWidth()<=1795) {
			
		}

		
        System.out.println(im1.getHeight());
        System.out.println(im1.getWidth());
        System.out.println("---------------------");
        
        System.out.println(im2.getHeight());
        System.out.println(im2.getWidth());
        System.out.println("---------------------");
        
        System.out.println(im3.getHeight());
        System.out.println(im3.getWidth());
		
		Test1 est = new Test1();
		est.test();
	}
	
	public void test() throws IOException {
		
		
		//String code = "https://tmp.s.weshaketv.com/1234567/lizhao/qrcode_258.png";
		
		
		String content = "https://tmp.s.weshaketv.com/1234567/lizhao/weChatPlugBack.png";
		
		String code = "C:/Users/domains90/Desktop/qrcode_1280.png";
		BufferedImage image = ImageIO.read(new File(code)); 
		
		
		int createWidth = 0;
		int createHeigh = 0;
		
		
        //创建图片对象，纯白背景
        //BufferedImage image = null;
        BufferedImage image2 = null;
        
        try {
        	//插件码户码
//            URL url = new URL(code);
//            image = ImageIO.read(url);
            
            //背景图
            URL url2 = new URL(content);
            image2 = ImageIO.read(url2);
            createWidth = image2.getWidth();
            createHeigh = image2.getHeight();
            
        }catch (IOException e){
            e.printStackTrace();
        }
        
        System.err.println(createWidth);
        System.err.println(createHeigh);
        BufferedImage back = new BufferedImage(createWidth, createHeigh, BufferedImage.TYPE_4BYTE_ABGR);
        Graphics2D graphics = back.createGraphics();
        //绘制白色背景
        graphics.setColor(Color.WHITE);
        graphics.fillRect(0, 0, createWidth,createHeigh);
        
        //消除文字锯齿
        graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        //消除画图锯齿
        graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        
        //添加插件码
        graphics.drawImage(image, 60, 290, 620, 690, null);//往画布添加图片，参数有位置及图片宽高

        //添加背景
        graphics.drawImage(image2, 0, 0, createWidth, createHeigh, null);//往画布添加图片，参数有位置及图片宽高
        
        
        
        //长按识别文案
        graphics.setPaint(Color.RED);
        Font font = new Font("思源黑体",Font.BOLD,30);

                
        //激活电子户码文案
        int nextHgigth = 35;      
        int num = this.fontWrite(graphics,createWidth,200,"长按识别二维码 关注\"东塔哈村\"",16,nextHgigth,font);       
        Font font1 = new Font("思源黑体",Font.BOLD,40);
        graphics.setFont(font1);
        FontMetrics fontMetrics1 = graphics.getFontMetrics(font1);
        // 文字宽度
        int textWidth1 = fontMetrics1.stringWidth("激活电子户码");
        // 计算出中心点，并且绘制出文字 x 居中
        graphics.drawString("激活电子户码", createWidth/2 - textWidth1/2, 220+num*nextHgigth);
        
        //失效文案
        graphics.setPaint(Color.GRAY);
        Font font3 = new Font("思源黑体",Font.BOLD,20);      
        final int failureDay = 7;//失效天数
        Calendar cl = Calendar.getInstance();
        cl.add(Calendar.DAY_OF_MONTH, +failureDay);//+1今天的时间加一天
        int month =cl.get(Calendar.MONTH) + 1;   //获取月份，0表示1月份
        int day = cl.get(Calendar.DAY_OF_MONTH);//获取当前天数
        String msg = String.format("该二维码%d天内（%d月%d日前）有效重新获取将更新", failureDay,month,day);
        this.fontWrite(graphics,createWidth,900,msg,17,nextHgigth,font3);

        
        graphics.dispose();
        ImageIO.write(back, "png", new File("C:/Users/domains90/Desktop/ccc.png"));
        System.out.println("生成结束");
	}
	
	/**
	 * @param graphics Graphics2D画布对象
	 * @param backWidth 背景宽
	 * @param y 文字从y轴什么地方写起
	 * @param msg 需要写入的文字
	 * @param length 按实际长度进行切割 写入
	 * @param nextHeigh 下一个高度
	 * @param font 字体
	 * @return 写入了几次
	 * */
	private int fontWrite(Graphics2D graphics,int backWidth,int y,String msg,int length,int nextHeigh,Font font) {
		msg = StringUtils.isBlank(msg)?"":msg;
        
        graphics.setFont(font);
        FontMetrics fontMetrics = graphics.getFontMetrics(font);


        BigDecimal aBig = new BigDecimal(msg.length());
        BigDecimal bBig = new BigDecimal(length);
      //向上取整
        int num1 = aBig.divide(bBig,BigDecimal.ROUND_UP ).intValue();
        for(int i=0;i<num1;i++) {
        	String ms = msg.substring(i*length, (i+1)*length>msg.length()?msg.length():(i+1)*length);
            // 文字宽度
            int textWidth = fontMetrics.stringWidth(ms);
            graphics.drawString(ms, backWidth/2 - textWidth/2, y+i*nextHeigh);
        }
        return num1;
	}

}
